package cn.libinal.scloud.base.common.web;

import lombok.Data;
import lombok.Getter;
import lombok.ToString;

import java.time.LocalDateTime;

/**
 * 页面返回数据封装
 *
 * @author libin
 * @param <T> 数据类型
 */
@ToString
@Data
public final class ResultJson<T> {
    private Integer status;
    private String message;
    private LocalDateTime timestamp = LocalDateTime.now();
    private T data;

    private ResultJson() {

    }
    private ResultJson(Integer status, String message, T data) {
        this();
        this.status = status;
        this.message = message;
        this.data = data;
    }

    public static <T> ResultJson<T> success(T data) {
        return new ResultJson<>(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getText(), data);
    }

    public static <T> ResultJson<T> error(T data) {
        return new ResultJson<>(ResultEnum.ERROR.getCode(), ResultEnum.ERROR.getText(), data);
    }

    public static <T> ResultJson<T> error(Integer status, String message) {
        return new ResultJson<>(status, message, null);
    }

    /**
     * 结果类型
     */
    @Getter
    public enum ResultEnum {
        /**
         * 成功
         */
        SUCCESS(1, "操作成功"),
        /**
         * 失败
         */
        ERROR(0, "操作失败");

        private Integer code;
        private String text;

        ResultEnum(Integer code, String text) {
            this.code = code;
            this.text = text;
        }
    }
}
